package api

import (
	"context"
	"fmt"
	"github.com/kataras/iris/v12/mvc"
	"micro-grpc-server/app/http/controllers"
	"micro-grpc-server/app/http/middleware"
	pb "micro-grpc-server/services/user/proto"
	wechat "micro-grpc-server/services/wechat/proto"
)

type UserController struct {
	controllers.Controller
	UserClient pb.UserService
}

func (c *UserController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/register", "Register")
	b.Handle("POST", "/login", "Login")
	b.Handle("POST", "/token", "Token")
	b.Handle("GET", "/profile", "Profile", middleware.MustLogin.Process)
	b.Handle("GET", "/share", "Share")
}

// Register
// @Router /user/register [post]
// @Summary 用户注册
// @Description 用户注册
// @Tags User
// @Produce json
// @Param body body user.RegisterRequest true "提交的数据"
// @Success 200 {array} user.TokenResponse
func (c *UserController) Register(ctx context.Context) {
	req := &pb.RegisterRequest{}
	if err := c.ReadJson(req); err != nil {
		c.BadRequest("请求参数错误->" + err.Error())
		return
	}
	res, err := c.UserClient.Register(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败! -> %s", c.FriendlyRpcError(err)))
		return
	}
	fmt.Println(res)
	c.JsonSuc(res)
}

// Login
// @Router /user/login [post]
// @Summary 密码登录
// @Description 密码登录
// @Tags User
// @Produce json
// @Param body body user.LoginRequest true "提交的数据"
// @Success 200 {array} user.TokenResponse
func (c *UserController) Login(ctx context.Context) {
	req := &pb.LoginRequest{}
	if err := c.ReadJson(req); err != nil {
		c.BadRequest("请求参数错误->" + err.Error())
		return
	}
	res, err := c.UserClient.LoginByPassword(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败! -> %s", c.FriendlyRpcError(err)))
		return
	}
	fmt.Println(res)
	c.JsonSuc(res)
}

// Token
// @Router /user/token [post]
// @Summary 微信登录
// @Description 微信登录
// @Tags User
// @Produce json
// @Param body body user.LoginRequest true "提交的数据"
// @Success 200 {array} user.TokenResponse
func (c *UserController) Token(ctx context.Context) {
	req := &wechat.WechatUserInfoRequest{}
	if err := c.ReadJson(req); err != nil {
		c.BadRequest("请求参数错误->" + err.Error())
		return
	}
	res, err := c.UserClient.LoginByWechat(ctx, req)
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败! -> %s", c.FriendlyRpcError(err)))
		return
	}
	fmt.Println(res)
	c.JsonSuc(res)
}

// Profile
// @Router /user/token [post]
// @Summary 微信登录
// @Description 微信登录
// @Tags User
// @Produce json
// @Success 200 {array} user.User
func (c *UserController) Profile(ctx context.Context) {
	res, err := c.UserClient.UserProfile(ctx, &pb.UserIdRequest{UserId: c.UserId()})
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败! -> %s", c.FriendlyRpcError(err)))
		return
	}
	fmt.Println(res)
	c.JsonSuc(res)
}

// Share
// @Router /user/share [get]
// @Summary 用户分享图
// @Description 用户分享图
// @Tags User
// @Produce json
// @Success 200
func (c *UserController) Share(ctx context.Context) {
	res, err := c.UserClient.UserShare(ctx, &pb.UserIdRequest{UserId: c.UserId()})
	if err != nil {
		c.BadRequest(fmt.Sprintf("请求失败! -> %s", c.FriendlyRpcError(err)))
		return
	}
	c.WriteImageResource(res.Image)
}
